<?php defined('BASEPATH') || exit('No direct script access allowed');
/**
 *
 * 后台工作人员
 *
 * @package   Bonfire
 * @author    liuzw@usuretech.com
 */

class Staff extends Front_Controller {
    
    /**
     * Setup the required libraries etc.
     *
     * @retun void
     */
    public function __construct() {
        parent::__construct();

        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->load->model('users/user_model');

        $this->load->library('users/auth');

        $this->lang->load('users');
        $this->siteSettings = $this->settings_lib->find_all();
        if ($this->siteSettings['auth.password_show_labels'] == 1) {
            Assets::add_module_js('users', 'password_strength.js');
            Assets::add_module_js('users', 'jquery.strength.js');
        }
    }

    // -------------------------------------------------------------------------
    // Authentication (Login/Logout)
    // -------------------------------------------------------------------------

    /**
     * 工作人员登录，成功之后跳转到后台页面.
     *
     * @return void
     */
    public function login()
    {
        // If the user is already logged in, go home.
        if ($this->auth->is_logged_in() !== false) {
            // Template::redirect('/'.SITE_AREA);
        }

        // Try to login.
        if (isset($_POST['log-me-in'])
            && true === $this->auth->login(
                $this->input->post('login'),
                $this->input->post('password'),
                $this->input->post('remember_me') == '1'
            )
        ) {
            log_activity(
                $this->auth->user_id(),
                lang('us_log_logged') . ': ' . $this->input->ip_address(),
                'users'
            );

            // Now redirect. (If this ever changes to render something, note that
            // auth->login() currently doesn't attempt to fix `$this->current_user`
            // for the current page load).

            // If the site is configured to use role-based login destinations and
            // the login destination has been set...
            if ($this->settings_lib->item('auth.do_login_redirect')
                && ! empty($this->auth->login_destination)
            ) {
                Template::redirect($this->auth->login_destination);
            }

            // If possible, send the user to the requested page.
            if (! empty($this->requested_page)) {
                // Template::redirect($this->requested_page);
            }

            // If there is nowhere else to go, go home.
            Template::redirect('/'.SITE_AREA);
        }

        // Prompt the user to login.
        // Template::set('page_title', 'Login');
        // Template::render('login');

        $this->load->view('staff/login');
        // Template::load_view('staff/login', null, '', false, $output);
    }

    /**
     * 退出登录.
     *
     * @return void
     */
    public function logout()
    {
        if (isset($this->current_user->id)) {
            // Login session is valid. Log the Activity.
            log_activity(
                $this->current_user->id,
                lang('us_log_logged_out') . ': ' . $this->input->ip_address(),
                'users'
            );
        }

        // Always clear browser data (don't silently ignore user requests).
        $this->auth->logout();
        Template::redirect('/'.SITE_AREA.'/login');
    }
}


?>